//
//  LGKNoteEditDataModel.m
//  LGKNote
//
//  Created by 刘亚军 on 2023/5/29.
//

#import "LGKNoteEditDataModel.h"
#import "LGKConst.h"
#import "LGKNoteRespondeModel.h"
#import "LGKNoteEditModel.h"
@implementation LGKNoteEditDataModel
- (void)yj_loadDataWithSuccess:(void (^)(BOOL))success failed:(void (^)(NSError * _Nonnull))failed{
    NSString *urlStr = [LGKNoteManager.defaultManager.noteApi yj_stringByAppendingPathComponent:@"api/Note/GetNoteDetail"];
    NSDictionary *params = @{@"UserID":kApiParams(LGKNoteManager.defaultManager.userID),
                             @"NoteID":kApiParams(self.resID),
                             @"Token":kApiParams(LGKNoteManager.defaultManager.token)};
    __weak typeof(self) weakSelf = self;
    [[YJNetManager defaultManager].setRequest(urlStr).setRequestType(YJRequestTypeGET).setParameters(params) startRequestWithSuccess:^(id  _Nonnull response) {
        LGKNoteRespondeModel *resModel = [LGKNoteRespondeModel responseModelWithData:response];
        if (resModel.isSuccess && !IsArrEmpty(resModel.Data)) {
            [weakSelf yj_handleResponseData:resModel.Data modelClass:LGKNoteEditModel.class success:success];
        }else{
            success(YES);
        }
    } failure:^(NSError * _Nonnull error) {
        failed(error);
    }];
}
- (void)uploadImgWithImgArr:(NSArray *)imgArr progressBlock:(void (^)(CGFloat))progressBlock complete:(void (^)(NSArray * _Nullable))complete{
    YJUploadModel *uploadModel = [[YJUploadModel alloc] init];
    NSMutableArray *imageDatas = [NSMutableArray array];
    NSMutableArray *fileNames = [NSMutableArray array];
    for (UIImage *image in imgArr) {
        [fileNames addObject:[NSString stringWithFormat:@"%@%li.png",[[NSUUID UUID] UUIDString],imageDatas.count]];
        NSData *imgData = [[UIImage yj_fixOrientation:image] yj_compressImageOnlength:200];
        [imageDatas addObject:imgData];
    }
    uploadModel.uploadDatas = imageDatas;;
    uploadModel.name = @"file";
    uploadModel.fileNames = fileNames;
    uploadModel.fileType = @"image/png";
    
    NSString *url = [[LGKNoteManager defaultManager].noteApi yj_stringByAppendingPathComponent:@"api/Note/UploadImg"];
 
    [[YJNetManager defaultManager].setRequest(url).setRequestType(YJRequestTypeUpload).setUploadModel(uploadModel) startRequestWithProgress:^(NSProgress *progress) {
        NSLog(@"YJNetManager上传进度值:%f",progress.fractionCompleted);
        if (progressBlock){
            progressBlock(progress.fractionCompleted);
        }
    } success:^(id response) {
        NSLog(@"成功：%@",response);
        LGKNoteRespondeModel *resModel = [LGKNoteRespondeModel responseModelWithData:response];
        if (resModel.isSuccess && !IsArrEmpty(resModel.Data)) {
            NSArray *imagesUrls = resModel.Data;
            if (complete) {
                complete(imagesUrls);
            }
        }else{
            if (complete) {
                complete(nil);
            }
        }
    } failure:^(NSError *error) {
        if (complete) {
            complete(nil);
        }
    }];
}
- (void)saveNoteWithParams:(NSDictionary *)params completion:(nonnull void (^)(NSString * _Nullable))completion{
    NSString *url = [[LGKNoteManager defaultManager].noteApi yj_stringByAppendingPathComponent:[NSString stringWithFormat:@"api/Note/SavePerNote?UserID=%@&Token=%@",LGKNoteManager.defaultManager.userID,LGKNoteManager.defaultManager.token]];
    [[YJNetManager defaultManager].setRequest(url).setRequestType(YJRequestTypePOST).setParameters(params) startRequestWithSuccess:^(id  _Nonnull response) {
        LGKNoteRespondeModel *resModel = [LGKNoteRespondeModel responseModelWithData:response];
        if (resModel.isSuccess) {
            if (completion) {
                completion(resModel.Data);
            }
        }else{
            if (completion) {
                completion(nil);
            }
        }
    } failure:^(NSError * _Nonnull error) {
        if (completion) {
            completion(nil);
        }
    }];
}
@end
